<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/12/10
 * Time: 14:12
 */

namespace app\api\controller;


use think\Db;

class Mycar extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    private $table = 'up_my_car';
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 我的车辆列表
     * @return \think\response\Json
     * page    页码
     */
    public function carList(){
        $list = Db::table($this->table)
            ->where('mc_user_id', $this->user_id)
            ->field('mc_id, mc_paizhao, mc_chejia, mc_engine, mc_status')
            ->order('mc_time desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的车辆详情
     * @return \think\response\Json
     * mc_id     我的车辆ID
     */
    public function showCar(){
        $mc_id = input('mc_id');
        $info = Db::table($this->table)
            ->where('mc_id', $mc_id)
            ->field('mc_id, mc_paizhao, mc_chejia, mc_engine, mc_status')
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 添加车辆
     * @return \think\response\Json
     * mc_paizhao     牌照
     * mc_chejia      车架号
     * mc_engine      发动机号
     */
    public function insertMyCar(){
        $mc_paizhao = trim(input('mc_paizhao'), ' ');
        $mc_chejia = trim(input('mc_chejia'), ' ');
        $mc_engine = trim(input('mc_engine'), ' ');
        
        if(!$mc_paizhao || !$mc_chejia || !$mc_engine) return json(['status' => 0, 'msg' => '请完善信息']);
        $have_paizhao = Db::table($this->table)->where('mc_paizhao', $mc_paizhao)->find();
        if($have_paizhao) return json(['status' => 0, 'msg' => '牌照已存在']);
        $have_chejia = Db::table($this->table)->where('mc_chejia', $mc_chejia)->find();
        if($have_chejia) return json(['status' => 0, 'msg' => '车架号已存在']);
        $have_engine = Db::table($this->table)->where('mc_engine', $mc_engine)->find();
        if($have_engine) return json(['status' => 0, 'msg' => '发动机号已存在']);
        
        $insert = [
            'mc_user_id' => $this->user_id,
            'mc_paizhao' => $mc_paizhao,
            'mc_chejia' => $mc_chejia,
            'mc_engine' => $mc_engine,
            'mc_time' => $this->create_time
        ];
        $res = Db::table($this->table)->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '添加成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 编辑我的车辆
     * @return \think\response\Json
     * mc_id        我的车辆ID
     * mc_paizhao   牌照
     * mc_chejia    车架
     * mc_engine    发动机号
     */
    public function updateMyCar(){
        $mc_id = input('mc_id');
        $mc_paizhao = trim(input('mc_paizhao'), ' ');
        $mc_chejia = trim(input('mc_chejia'), ' ');
        $mc_engine = trim(input('mc_engine'), ' ');
        
        if(!$mc_paizhao || !$mc_chejia || !$mc_engine) return json(['status' => 0, 'msg' => '请完善信息']);
        $have_paizhao = Db::table($this->table)->where('mc_paizhao', $mc_paizhao)->where('mc_id', 'NEQ', $mc_id)->find();
        if($have_paizhao) return json(['status' => 0, 'msg' => '牌照已存在']);
        $have_chejia = Db::table($this->table)->where('mc_chejia', $mc_chejia)->where('mc_id', 'NEQ', $mc_id)->find();
        if($have_chejia) return json(['status' => 0, 'msg' => '车架号已存在']);
        $have_engine = Db::table($this->table)->where('mc_engine', $mc_engine)->where('mc_id', 'NEQ', $mc_id)->find();
        if($have_engine) return json(['status' => 0, 'msg' => '发动机号已存在']);
    
        $update = [
            'mc_paizhao' => $mc_paizhao,
            'mc_chejia' => $mc_chejia,
            'mc_engine' => $mc_engine,
            'mc_updatetime' => $this->create_time
        ];
        $res = Db::table($this->table)->where('mc_id', $mc_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 上报车辆
     * @return \think\response\Json
     */
    public function upCarStatus(){
        $mc_ids = Db::table('up_my_car')
            ->where('mc_user_id', $this->user_id)
            ->where('mc_status', 1)
            ->column('mc_id');
        if($mc_ids){
            $res = Db::table('up_my_car')
                ->where('mc_id', 'IN', $mc_ids)
                ->setField('mc_status', 2);
            if($res){
                return json(['status' => 200, 'msg' => '操作成功']);
            }else{
                return json(['status' => 0, 'msg' => '操作失败']);
            }
        }else{
            return json(['status' => 200, 'msg' => '没有需要操作的车辆']);
        }
    }
}